package com.xhapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.xhapi.config.XfXhConfig;
import com.xhapi.pojo.dto.ChoiceQuestionByAiDTO;
import com.xhapi.pojo.entity.ChoiceQuestionsByAi;
import com.xhapi.mapper.ChoiceQuestionsByAiMapper;
import com.xhapi.pojo.dto.ChoiceQuestionsByAiWithOption;
import com.xhapi.pojo.vo.ChoiceQuestionByAiVO;
import com.xhapi.service.IChoiceQuestionsByAiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhapi.utils.knowledgeBaseUtils.ChatDocUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 由知识库提供的单选题 服务实现类
 * </p>
 *
 * @author 179005
 * @since 2024-06-11
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ChoiceQuestionsByAiServiceImpl extends ServiceImpl<ChoiceQuestionsByAiMapper, ChoiceQuestionsByAi> implements IChoiceQuestionsByAiService {
    private final ChatDocUtil chatDocUtil;
    private final XfXhConfig xfXhConfig;
    private static final String chatUrl = "wss://chatdoc.xfyun.cn/openapi/chat";
    @Override
    @Transactional
    public List<ChoiceQuestionsByAi> createQA(ChoiceQuestionByAiDTO dto) {
        String [] fileIds = {
                "d9d45d47ead645b1801f5276dc90a6d0",
                "c338f13d843945f288be7fd5f1db9d2c",
                "dd73a010e35943fab61f84c97d194fb6",
                "c224a766845c48589a421c5b092035f4"
        };
        String question= """
                请你根据文件，帮我生成1道选择题，要求包含以下知识点：%s，要求的输出格式为
                [{
                    "question":"生成的问题",
                    "option":"四个选项，请不要使用[]",
                    "difficulty":"生成的难度1",
                    "answer":"生成的答案1",
                    "analysis":"生成的分析1",
                    "knowledgePoint":"相关知识点1"
                }]
               ，请不要生成任何额外多余内容
                """.formatted(dto.getPointList());
        String chat = chatDocUtil.chat(chatUrl, fileIds, question, "f7f9a95b", "ZDRjMDhjYjBkZjFjOTU5NmI1ZWNiOWRl");
        log.info("chat:{}",chat);
        List<ChoiceQuestionsByAi>choiceQuestionsByAiList=new ArrayList<>();
        Gson gson=new Gson();
        JsonParser jsonParser=new JsonParser();
        JsonArray jsonArray=jsonParser.parse(chat).getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            ChoiceQuestionsByAiWithOption choiceQuestionsByAiWithOption=gson.fromJson(jsonElement,ChoiceQuestionsByAiWithOption.class);
            ChoiceQuestionsByAi choiceQuestionsByAi=BeanUtil.copyProperties(choiceQuestionsByAiWithOption,ChoiceQuestionsByAi.class);
            choiceQuestionsByAi.setQuestion(choiceQuestionsByAiWithOption.getQuestion()+choiceQuestionsByAiWithOption.getOption());
            choiceQuestionsByAi.setUpdateTime(LocalDateTime.now().toString());
            choiceQuestionsByAi.setStatus(-1);
            choiceQuestionsByAi.setCreateUser(20224001);
            choiceQuestionsByAiList.add(choiceQuestionsByAi);
        }
        saveBatch(choiceQuestionsByAiList);
        // 这里只返回给前端 问题、难度、创建时间
        return choiceQuestionsByAiList;

    }

    // 返回给前端ChoiceQuestionsByAi的完整信息
    @Override
    public ChoiceQuestionsByAi answer(Integer id, String answer,Integer userId) {
        ChoiceQuestionsByAi one = getOne(new QueryWrapper<ChoiceQuestionsByAi>().eq("id", id).eq("create_user", userId));
        if(answer.equals(one.getAnswer())){
            one.setStatus(1);
        }else {
            one.setStatus(0);
        }
        one.setUpdateTime(LocalDateTime.now().toString());
        updateById(one);
        return one;
    }
}
